/*
 * @lc app=leetcode.cn id=643 lang=typescript
 *
 * [643] 子数组最大平均数 I
 */

// @lc code=start

// 常规暴力法会直接超时，而滑动窗口不会
function findMaxAverage(nums: number[], k: number): number {
    let max = nums.slice(0, k).reduce((a, b) => a + b, 0);
    let tail = 0;
    let long = max;
    for (let index = k; index < nums.length; index++) {
        const element = nums[index];
        long = long + element - nums[tail];
        max = Math.max(max, long);
        tail++;
    }

    return max / k;
}
// @lc code=end
